﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LucyGrid;
using System.Threading;

namespace LucyDemo.BasicLoadDistribution
{
	/// <summary>
	/// Distributes simple const processing
	/// </summary>
	class BasicLoadDistribution : AbstractDemo
	{
		public int LoadToDistribute;
		class Load{
			public string Data;
		}

		public BasicLoadDistribution() {
			
		}

		IEnumerable<Load> Generate() {
			for (var c = 0; c != LoadToDistribute; ++c)
				yield return new Load()
				{
					Data = string.Format("Load {0}", c)
				};
		}

		override
		public string Info() {
			return "Process several jobs with a constant processing time over the grid.";
		}

		override
		public string Run(bool enableLocalProcessing)
		{
			var res = Generate()
				.AsParallelGrid(l =>
				{
					Thread.Sleep(300);
					return Environment.MachineName;
				}, new GridSettings() { 
					EnableLocalProcessing = enableLocalProcessing
				})
				.ToArray();
			if (res.Length != LoadToDistribute) throw new Exception("Error processing data");
			return string.Format("{0} jobs distributed between {1} nodes", res, res.Distinct().Count());
		}
	}
}
